| Conditions | 1 |
| Paths | 72 |
| Total Lines | 92 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /* |
||
| 34 | }(function ($) { |
||
| 35 | 'use strict'; |
||
| 36 | |||
| 37 | // Append to the default processQueue: |
||
| 38 | $.blueimp.fileupload.prototype.options.processQueue.push( |
||
| 39 | { |
||
| 40 | action: 'validate', |
||
| 41 | // Always trigger this action, |
||
| 42 | // even if the previous action was rejected: |
||
| 43 | always: true, |
||
| 44 | // Options taken from the global options map: |
||
| 45 | acceptFileTypes: '@', |
||
| 46 | maxFileSize: '@', |
||
| 47 | minFileSize: '@', |
||
| 48 | maxNumberOfFiles: '@', |
||
| 49 | disabled: '@disableValidation' |
||
| 50 | } |
||
| 51 | ); |
||
| 52 | |||
| 53 | // The File Upload Validation plugin extends the fileupload widget |
||
| 54 | // with file validation functionality: |
||
| 55 | $.widget('blueimp.fileupload', $.blueimp.fileupload, { |
||
| 56 | |||
| 57 | options: { |
||
| 58 | /* |
||
| 59 | // The regular expression for allowed file types, matches |
||
| 60 | // against either file type or file name: |
||
| 61 | acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i, |
||
| 62 | // The maximum allowed file size in bytes: |
||
| 63 | maxFileSize: 10000000, // 10 MB |
||
| 64 | // The minimum allowed file size in bytes: |
||
| 65 | minFileSize: undefined, // No minimal file size |
||
| 66 | // The limit of files to be uploaded: |
||
| 67 | maxNumberOfFiles: 10, |
||
| 68 | */ |
||
| 69 | |||
| 70 | // Function returning the current number of files, |
||
| 71 | // has to be overriden for maxNumberOfFiles validation: |
||
| 72 | getNumberOfFiles: $.noop, |
||
| 73 | |||
| 74 | // Error and info messages: |
||
| 75 | messages: { |
||
| 76 | maxNumberOfFiles: 'Maximum number of files exceeded', |
||
| 77 | acceptFileTypes: 'File type not allowed', |
||
| 78 | maxFileSize: 'File is too large', |
||
| 79 | minFileSize: 'File is too small' |
||
| 80 | } |
||
| 81 | }, |
||
| 82 | |||
| 83 | processActions: { |
||
| 84 | |||
| 85 | validate: function (data, options) { |
||
| 86 | if (options.disabled) { |
||
| 87 | return data; |
||
| 88 | } |
||
| 89 | var dfd = $.Deferred(), |
||
| 90 | settings = this.options, |
||
| 91 | file = data.files[data.index], |
||
| 92 | fileSize; |
||
| 93 | if (options.minFileSize || options.maxFileSize) { |
||
| 94 | fileSize = file.size; |
||
| 95 | } |
||
| 96 | if ($.type(options.maxNumberOfFiles) === 'number' && |
||
| 97 | (settings.getNumberOfFiles() || 0) + data.files.length > |
||
| 98 | options.maxNumberOfFiles) { |
||
| 99 | file.error = settings.i18n('maxNumberOfFiles'); |
||
| 100 | } else if (options.acceptFileTypes && |
||
| 101 | !(options.acceptFileTypes.test(file.type) || |
||
| 102 | options.acceptFileTypes.test(file.name))) { |
||
| 103 | file.error = settings.i18n('acceptFileTypes'); |
||
| 104 | } else if (fileSize > options.maxFileSize) { |
||
|
|
|||
| 105 | file.error = settings.i18n('maxFileSize'); |
||
| 106 | } else if ($.type(fileSize) === 'number' && |
||
| 107 | fileSize < options.minFileSize) { |
||
| 108 | file.error = settings.i18n('minFileSize'); |
||
| 109 | } else { |
||
| 110 | delete file.error; |
||
| 111 | } |
||
| 112 | if (file.error || data.files.error) { |
||
| 113 | data.files.error = true; |
||
| 114 | dfd.rejectWith(this, [data]); |
||
| 115 | } else { |
||
| 116 | dfd.resolveWith(this, [data]); |
||
| 117 | } |
||
| 118 | return dfd.promise(); |
||
| 119 | } |
||
| 120 | |||
| 121 | } |
||
| 122 | |||
| 123 | }); |
||
| 124 | |||
| 125 | })); |
||
| 126 |